﻿/*----------------------------------------------------------------
* 项目名称 ：EMSH
* 类 名 称 ：ReddotManager
* 类 描 述 ：红点管理器
* 作    者 ：Xu Donghao
* 创建时间 ：2023/2/21/周二 下午 03:37:30
* CLR 版本 ：4.0.30319.42000
* 版 本 号 ：v1.0.0.0
*******************************************************************
* Copyright @ Xu Donghao 2023. All rights reserved.
*******************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using UnityEngine;


/// <summary>
/// 红点管理器
/// </summary>
public class ReddotManager
{
    public static ReddotManager Instance;

    private ReddotAssetData m_ReddotData;
    private Dictionary<string, ReddotData> m_ReddotDic = new Dictionary<string, ReddotData>();

    private Dictionary<string, List<Reddot>> m_CurrentReddot = new Dictionary<string, List<Reddot>>();

    private void Init()
    {
        m_ReddotData = Resources.Load<ReddotAssetData>("Reddot");

        if (m_ReddotData != null && m_ReddotData.Reddots != null)
        {
            for (int i = 0; i < m_ReddotData.Reddots.Count; i++)
            {
                if (m_ReddotDic.ContainsKey(m_ReddotData.Reddots[i].ID)) continue;

                m_ReddotDic.Add(m_ReddotData.Reddots[i].ID, m_ReddotData.Reddots[i]);
            }
        }
    }

    private void Reset()
    {
        foreach (var item in m_ReddotDic)
        {
            item.Value.Reset();
        }

        CheckAll();
    }

    private void Start()
    {
        Instance = this;

        Init();

        Reset();
    }

    public void Register(string id, Reddot reddot)
    {
        if (!m_CurrentReddot.ContainsKey(id))
        {
            m_CurrentReddot.Add(id, new List<Reddot>());
        }

        m_CurrentReddot[id].Add(reddot);

        Check(id);
    }

    public void Unregister(string id, Reddot reddot)
    {
        if (!m_CurrentReddot.ContainsKey(id) || !m_CurrentReddot[id].Contains(reddot)) return;

        m_CurrentReddot[id].Remove(reddot);

        Check(id);
    }

    public void Show(string id, int number = 1)
    {
        if (!m_ReddotDic.ContainsKey(id))
        {
            Debug.LogError(string.Format("红点未添加:{0}", id));
            return;
        }

        var reddot = m_ReddotDic[id];
        ChangeNumber(reddot, number);
    }

    public void Hide(string id)
    {
        if (!m_CurrentReddot.ContainsKey(id) || !m_ReddotDic.ContainsKey(id)) return;

        var reddot = m_ReddotDic[id];
        ChangeNumber(reddot, 0);
    }
    
    private void ChangeNumber(ReddotData reddot, int number)
    {
        var change = number - reddot.Number;

        var current = reddot;
        for (int i = 0; i < m_ReddotData.DepthMax; i++)
        {
            current.Number += change;
            Check(current.ID);

            current = current.Parent;

            if (current == null) break;
        }
    }

    private void CheckAll()
    {
        foreach (var item in m_CurrentReddot)
        {
            if (!m_ReddotDic.ContainsKey(item.Key)) continue;

            var data = m_ReddotDic[item.Key];
            for (int i = 0; i < item.Value.Count; i++)
            {
                item.Value[i].Refresh(data.Number);
            }
        }
    }

    private void Check(string id)
    {
        if (!m_CurrentReddot.ContainsKey(id) || !m_ReddotDic.ContainsKey(id)) return;

        var data = m_ReddotDic[id];

        var reddots = m_CurrentReddot[id];
        if (reddots.Count <= 0) return;

        for (int i = 0; i < reddots.Count; i++)
        {
            reddots[i].Refresh(data.Number);
        }
    }
}